#include "WindowsAPIInput.h"

//Standard library headers
#include <windows.h>

#include <iostream>

WindowsAPIInput::WindowsAPIInput()
{
    //Get the mouse location
	POINT pos;
	if (GetCursorPos(&pos) != 0) { 
	    //Save the new mouse position
		mousepos = Vector(pos.x, pos.y);
    }
    
    for (int i = 0; i != IInput::MAX_KEYS; ++i){
        oldkeys[i] = false;
        keys[i] = false;
    }
}

void WindowsAPIInput::update()
{
    //Get new key states
    for (int i = 0; i != IInput::MAX_KEYS; ++i){
		oldkeys[i] = keys[i];
		keys[i] = (GetAsyncKeyState(i) & 0x8000);
	}
	
	//Get the mouse location
	POINT pos;
	if (GetCursorPos(&pos) != 0) { 
	    //Store the old position of the mouse
	    mouseoldpos = mousepos;
	    
	    //Save the new mouse position
		mousepos = Vector(pos.x, pos.y);
	    
	    //Find the vector of movement
	    mousevector = mousepos - mouseoldpos;
	    
	    //Get the distance moved
	    mousedistance = mousevector.getLength();
	    
	    //If the mouse moved
	    if (mousedistance > 0){
			//Store the mouse direction
			mouseangle = mousevector.getAngle();
		}
	}
	else{
		std::cerr << "Error in WindowsAPIInput: GetCursorPos failed." << std::endl;
	}
}
